<?php

require_once( dirname(__FILE__) . '/htmlpurifier/HTMLPurifier.auto.php' );

class Services_HTML
{
	private static $purifier = null;
	/*
	 *过滤html中的危险元素
	 *@param $dirty string 过滤前的html
	 *@return string 过滤后的html
	 */
	static public function filte($dirty){
		if(Services_HTML::$purifier == null)
		{
			$config = HTMLPurifier_Config::createDefault();
			//!NOTICE 允许iframe元素
			$config->set('HTML.SafeIframe', true);
			//iframe 白名单规则
			//TODO
			$iframe_whitelist = array(
				'player.youku.com/',
				'player.56.com/',
				'www.youku.com/',
				'www.56.com/',
				'ssl.acfun.tv/',
				'music.163.com/',
				'www.tudou.com/',
				'v.qq.com/',
				'douban.fm/'
			);
			$iframe_whitereg = '%^(http|https)://('.implode('|', $iframe_whitelist).')%';
			$config->set('URI.SafeIframeRegexp', $iframe_whitereg);
			//页面锚点连接
			$config->set('Attr.EnableID', true);
			//规定除以anchor_为前缀的id为合法外其他的都会被过滤
			$config->set('Attr.IDBlacklistRegexp', '%^(?!anchor_)%');
			Services_HTML::$purifier = new HTMLPurifier($config);
		}

		return Services_HTML::$purifier->purify($dirty);
	} 
}